package cn.source.zk.service.impl;

import cn.source.zk.domain.entity.Channel;
import cn.source.zk.domain.entity.Device;
import cn.source.zk.domain.entity.Sort;
import cn.source.zk.domain.vo.DeviceAndChannelByTdGuidVO;
import cn.source.zk.mapper.ZkChannelMapper;
import cn.source.zk.mapper.ZkCxdzMapper;
import cn.source.zk.mapper.ZkDeviceMapper;
import cn.source.zk.service.IZkChannelService;
import cn.source.zk.service.IZkChannelSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ZkChannelServiceImpl implements IZkChannelService {

    @Autowired
    private ZkChannelMapper channelMapper;

    @Autowired
    private ZkDeviceMapper deviceMapper;

    @Autowired
    private ZkCxdzMapper cxdzMapper;

    @Autowired
    private IZkChannelSortService channelSortService;

    /**
     * 查询通道分页列表
     *
     * @param channel
     * @return
     */
    public List<Channel> listPage(Channel channel) {

        return channelMapper.getListPage(channel);
    }

    /**
     * 新增通道
     *
     * @param channel
     * @return
     */
    public int add(Channel channel) {

        //根据设备id查询zhGuid，
        Device device = deviceMapper.getByNum(channel.getDeviceNum());
        channel.setZhGuid(device.getZhGuid());

        return channelMapper.add(channel);
    }

    /**
     * 根据num查询通道
     *
     * @param num
     * @return
     */
    public Channel getByNum(Long num) {

        return channelMapper.getByNum(num);
    }

    /**
     * 修改通道
     *
     * @param channel
     * @return
     */
    public int edit(Channel channel) {
        return channelMapper.edit(channel);
    }

    /**
     * 删除通道
     *
     * @param nums
     * @return
     */
    @Transactional
    public int remove(List<Long> nums) {
        // 删除的时候需要把cxdz中的内容也删除掉；
        List<String> tdGuids = new ArrayList<>();
        for (Long num : nums) {
            tdGuids.add(channelMapper.getByNum(num).getTdGuid());
        }
        cxdzMapper.removeBatch(tdGuids);
        return channelMapper.remove(nums);
    }

    /**
     * 检查通道名称是否存在
     *
     * @param channelName
     * @param num
     * @return
     */
    public Boolean checkChannelNameExist(String channelName, Long num, Long deviceNum) {
        Channel channel = new Channel();
        channel.setChannelName(channelName);
        channel.setDeviceNum(deviceNum);
        channel.setNum(num);

        int result = channelMapper.checkChannelNameExist(channel);

        return result > 0;
    }

    /**
     * 根据tdGuid获取设备ID
     *
     * @param tdGuid
     * @return
     */
    public String getDeviceByTdGuid(String tdGuid) {
        return channelMapper.getDeviceByTdGuid(tdGuid);
    }

    /**
     * 根据deviceNum列表获取通道列表
     *
     * @param nums
     * @return
     */
    public List<Channel> getChannelByDeviceNums(List<Long> nums) {
        return channelMapper.getChannelByDeviceNums(nums);
    }

    /**
     * 根据并网点num列表查询channel
     *
     * @param nums
     * @return
     */
    public List<Channel> getByConnectionPointNums(List<Long> nums) {
        return channelMapper.getByConnectionPointNums(nums);
    }


    /**
     * 根据tdGuids获取通道列表
     *
     * @param tdGuids
     * @return
     */
    public List<Channel> getByTdGuids(List<String> tdGuids) {
        return channelMapper.getByTdGuid(tdGuids);
    }

    @Override
    public List<Channel> getChannelListByDeviceNums(ArrayList<Long> deviceNums) {
        return channelMapper.getChannelListByDeviceNums(deviceNums);
    }

    @Override
    public List<Sort> getSortList(Long deviceNum) {
        // 查询已有的排序列表
        List<Long> sortIds = channelMapper.getChannelNumByDeviceNum(deviceNum);
        // 排除已有列表
        return channelSortService.exclusionSortList(sortIds);
    }

    @Override
    public DeviceAndChannelByTdGuidVO getDeviceAndChannelByTdGuid(String tdGuid) {

        // List<String> tdGuids = new ArrayList<>();
        // tdGuids.add(tdGuid);
        // List<Channel> byTdGuids = getByTdGuids(tdGuids);
        // Channel channel = byTdGuids.get(0);

        Channel channel = channelMapper.getChannelByTdGuid(tdGuid);

        Device device = deviceMapper.getByZhGuid(channel.getZhGuid());

        DeviceAndChannelByTdGuidVO vo = new DeviceAndChannelByTdGuidVO();
        vo.setDeviceName(device.getDevName());
        vo.setChannelName(channel.getChannelName());

        return vo;
    }
}
